clear all
set printcolor gs1, permanently
set more off
global dirout ""
global dirgeolocalized ""
global dirmarco ""
global dirtaximeter ""


use "$dirgeolocalized/collapsed_uber_censo_lessthan500mts.dta" if year<2018, clear
egen trips=rowtotal(trips_card trips_cash)
egen fares=rowtotal(fares_card fares_cash)
gen share_cash=fares_cash/fares

tsset ID year
tsfill, full
replace trips=0 if trips==.
replace fares=0 if fares==.
keep trips fares share_cash POBTOT year CVE_ENT CVE_MUN CVE_LOC CVE_AGEB CVE_MZA GRAPROES VPH_CEL VPH_INTER VPH_AUTOM VIVTOT

preserve
collapse (mean) share_cash [w=POBTOT], by(year CVE_ENT CVE_MUN)
replace share_cash=. if CVE_ENT=="09"
keep if CVE_ENT=="15"
keep if year==2017
replace share_cash=0 if share_cash==.
drop year
merge 1:1 CVE_ENT CVE_MUN using "$dirmarco/15_/conjunto de datos/15mun_db.dta", nogenerate keep(match)
spmap share_cash using "$dirmarco/15_/conjunto de datos/15mun_cor", id(id) fcolor(Blues) 
graph export "$dirout/FigureC8a.eps", replace
restore 

collapse (sum) trips fares (mean) share_cash [w=POBTOT], by(year CVE_ENT CVE_MUN)

egen id_temp=group(CVE_ENT CVE_MUN)
tsset id year
tsfill, full
replace trips=0 if trips==.
replace fares=0 if fares==.
gen d_trips=2*(trips-l.trips)/(trips+l.trips)
gen d_fares=2*(fares-l.fares)/(fares+l.fares)
keep if year==2017
drop year

preserve 
merge 1:1 CVE_ENT CVE_MUN using "$dirmarco/15_/conjunto de datos/15mun_db.dta", nogenerate keep(match)
spmap d_trips using "$dirmarco/15_/conjunto de datos/15mun_cor", id(id) fcolor(Blues) 
graph export "$dirout/FigureC8b.eps", replace
restore



use "$dirgeolocalized/Puebla/collapsed_uber_censo_lessthan500mts_Puebla.dta", clear
egen trips=rowtotal(trips_card trips_cash)
egen fares=rowtotal(fares_card fares_cash)
gen share_cash=fares_cash/fares
tsset ID year
tsfill, full
replace trips=0 if trips==.
replace fares=0 if fares==.
keep trips fares share_cash POBTOT year CVE_ENT CVE_MUN CVE_LOC CVE_AGEB CVE_MZA GRAPROES VPH_CEL VPH_INTER VPH_AUTOM VIVTOT

preserve
collapse (sum) trips fares (mean) share_cash [w=POBTOT], by(year CVE_ENT CVE_MUN CVE_LOC CVE_AGEB)
egen id_temp=group(CVE_ENT CVE_MUN CVE_LOC CVE_AGEB)
tsset id year
tsfill, full
by id: carryforward CVE_ENT CVE_MUN CVE_LOC CVE_AGEB, replace
replace trips=0 if trips==.
replace fares=0 if fares==.
gen d_trips=2*(trips-l.trips)/(trips+l.trips)
gen d_fares=2*(fares-l.fares)/(fares+l.fares)
gsort id -year
by id : replace CVE_MUN = CVE_MUN[_n-1] if missing(CVE_MUN)
by id : replace CVE_LOC = CVE_LOC[_n-1] if missing(CVE_LOC)
by id : replace CVE_AGEB = CVE_AGEB[_n-1] if missing(CVE_AGEB)
keep if year==2017
duplicates drop
drop year
merge 1:1 CVE_ENT CVE_LOC CVE_MUN CVE_AGEB using "$dirmarco/21_/conjunto de datos/21a_db.dta", nogenerate keep(match)

spmap d_trips using "$dirmarco/21_/conjunto de datos/21a_cor", id(id) fcolor(Blues) 
graph export "$dirout/FigureD5a.eps", replace
restore


preserve
collapse (sum) trips fares (mean) share_cash [w=POBTOT], by(year CVE_ENT CVE_MUN CVE_LOC CVE_AGEB)
egen id_temp=group(CVE_ENT CVE_MUN CVE_LOC CVE_AGEB)
tsset id year
tsfill, full
by id: carryforward CVE_ENT CVE_MUN CVE_LOC CVE_AGEB, replace
replace trips=0 if trips==.
replace fares=0 if fares==.
gen d_trips=2*(trips-l.trips)/(trips+l.trips)
gen d_fares=2*(fares-l.fares)/(fares+l.fares)
replace CVE_ENT="21" if CVE_ENT==""
gsort id -year
by id : replace CVE_MUN = CVE_MUN[_n-1] if missing(CVE_MUN)
by id : replace CVE_LOC = CVE_LOC[_n-1] if missing(CVE_LOC)
by id : replace CVE_AGEB = CVE_AGEB[_n-1] if missing(CVE_AGEB)
keep if year==2018
duplicates drop
drop year
merge 1:1 CVE_ENT CVE_LOC CVE_MUN CVE_AGEB using "$dirmarco/21_/conjunto de datos/21a_db.dta", nogenerate keep(match)

spmap d_trips using "$dirmarco/21_/conjunto de datos/21a_cor", id(id) fcolor(Reds) 
graph export "$dirout/FigureD5b.eps", replace
restore


use "BDIF.dta" if year == 2017 & state == "09", clear
ren municipio CVE_MUN
collapse (sum) population debit branches, by(CVE_MUN)
replace debit = debit/population
replace branches = branches/(population/10000)
format debit %4.2f
format branches %4.2f

merge 1:1 CVE_MUN using "09mun", nogen

spmap debit using "09muncoord", ///
	id(id) fcolor(Blues) legend(ring(1))
graph export  "$dirout/FigureE2a.eps", replace

spmap branches using "09muncoord", ///
	id(id) fcolor(Blues) legend(ring(1))
graph export  "$dirout/FigureE3a.eps", replace


use "BDIF.dta" if year == 2017 & state == "15", clear
ren municipio CVE_MUN
collapse (sum) population debit branches, by(CVE_MUN)
replace debit = debit/population
replace branches = branches/(population/10000)
format debit %4.2f
format branches %4.2f

merge 1:1 CVE_MUN using "15mun", nogen

spmap debit using "15muncoord", ///
	id(id) fcolor(Blues) legend(ring(1))
graph export  "$dirout/FigureE2b.eps", replace

spmap branches using "15muncoord", ///
	id(id) fcolor(Blues) legend(ring(1))
graph export  "$dirout/FigureE3b.eps", replace



cd "$dirtaximeter"
use "municipios_mx_feb2018_db.dta", clear
keep if CVE_ENT=="09"
mergepoly id using "municipios_mx_feb2018_cor.dta", coor("cdmx_coor.dta") replace
		
use "trips_taximeter_with_census.dta", clear

gen trips=1
collapse (sum) trips, by(CVE_ENT_o CVE_MUN_o)
rename CVE_ENT_o CVE_ENT
rename CVE_MUN_o CVE_MUN

drop if CVE_MUN=="082"

merge 1:1 CVE_ENT CVE_MUN using "municipios_mx_feb2018_db.dta", keep(match) nogenerate

spmap trips using "municipios_mx_feb2018_cor.dta" if id!=., ///
id(id) fcolor(Blues) title("") legstyle(2) ///
legend(region(lcolor(white)))  plotregion(icolor(white)) graphregion(icolor(white)) ///
clnumber(5)  clmethod(quantile)   ocolor(none none none none none none) ///
legend(pos(5) row(5)) ///
polygon(data("cdmx_coor.dta") ocolor(black) )
graph export "$dirout/FigureF3b.eps", replace


